package com.edu.exam.entity.grading;

import com.edu.exam.entity.curriculum.classification.DifficultyLevel;
import com.edu.exam.enums.quality.monitoring.PriorityLevel;
import com.edu.exam.enums.quality.monitoring.ProcessingStatus;
import com.edu.exam.entity.curriculum.classification.QuestionType;
import com.edu.exam.enums.grading.GradingStatus;
import com.edu.exam.enums.grading.QualityCheckStatus;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 网上批阅图像实体类
 * 所属模块：B-06 成绩管理模块
 *
 * 根据E-04-05 网上批阅子模块设计实现
 * 对应grading_image数据表
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public class GradingImage {

    @Id
    @Column(name = "image_id", length = 64)
    private String imageId;

    /**
     * 任务ID
     */
    @Column(name = "task_id", nullable = false, length = 64)
    private String taskId;

    /**
     * 学生ID
     */
    @Column(name = "student_id", nullable = false, length = 64)
    private String studentId;

    /**
     * 考试ID
     */
    @Column(name = "exam_id", nullable = false, length = 64)
    private String examId;

    /**
     * 试卷ID
     */
    @Column(name = "paper_id", nullable = false, length = 64)
    private String paperId;

    /**
     * 题目ID
     */
    @Column(name = "question_id", nullable = false, length = 64)
    private String questionId;

    /**
     * 题号
     */
    @Column(name = "question_number", nullable = false, length = 32)
    private String questionNumber;

    /**
     * 题目类型：SUBJECTIVE（主观题）、OBJECTIVE（客观题）、MIXED（混合题）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "question_type", nullable = false, length = 32)
    private QuestionType questionType;

    /**
     * 图像URL
     */
    @Column(name = "image_url", nullable = false, length = 512)
    private String imageUrl;

    /**
     * 缩略图URL
     */
    @Column(name = "thumbnail_url", length = 512)
    private String thumbnailUrl;

    /**
     * 图像宽度
     */
    @Column(name = "image_width", nullable = false)
    private Integer imageWidth;

    /**
     * 图像高度
     */
    @Column(name = "image_height", nullable = false)
    private Integer imageHeight;

    /**
     * 文件大小（字节）
     */
    @Column(name = "file_size", nullable = false)
    private Long fileSize;

    /**
     * 批阅状态：PENDING（待批阅）、GRADING（批阅中）、COMPLETED（已完成）、SKIPPED（跳过）、REVIEWED（已复核）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "grading_status", nullable = false, length = 32)
    private GradingStatus gradingStatus;

    /**
     * 分配的教师ID
     */
    @Column(name = "assigned_teacher_id", length = 64)
    private String assignedTeacherId;

    /**
     * 得分
     */
    @Column(name = "score", precision = 5, scale = 2)
    private Double score;

    /**
     * 满分
     */
    @Column(name = "max_score", nullable = false, precision = 5, scale = 2)
    private Double maxScore;

    /**
     * 批阅时间
     */
    @Column(name = "grading_time")
    private LocalDateTime gradingTime;

    /**
     * 批阅耗时（秒）
     */
    @Column(name = "grading_duration")
    private Integer gradingDuration;

    /**
     * 质检状态：PENDING（待质检）、PASSED（通过）、FAILED（未通过）、REVIEWING（审核中）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "quality_check_status", length = 32)
    private QualityCheckStatus qualityCheckStatus;

    /**
     * 反馈文本
     */
    @Column(name = "feedback_text", length = 2000, columnDefinition = "TEXT")
    private String feedbackText;

    /**
     * 元数据（JSON格式）
     */
    @Column(name = "metadata", length = 2000, columnDefinition = "TEXT")
    private String metadata;

    /**
     * 图像质量分数（0-100）
     */
    @Column(name = "image_quality_score", precision = 5, scale = 2)
    private Double imageQualityScore;

    /**
     * 识别置信度（0-100）
     */
    @Column(name = "recognition_confidence", precision = 5, scale = 2)
    private Double recognitionConfidence;

    /**
     * 处理状态：PROCESSED（已处理）、PROCESSING（处理中）、FAILED（失败）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "processing_status", length = 32)
    private ProcessingStatus processingStatus;

    /**
     * 优先级：LOW（低）、NORMAL（普通）、HIGH（高）、URGENT（紧急）
     * 映射到PriorityLevel枚举
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "priority", length = 16)
    private PriorityLevel priority;

    /**
     * 难度等级：EASY（简单）、MEDIUM（中等）、HARD（困难）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "difficulty_level", length = 16)
    private DifficultyLevel difficultyLevel;

    /**
     * 是否需要特殊处理
     */
    @Column(name = "requires_special_handling")
    private Boolean requiresSpecialHandling;

    /**
     * 特殊处理原因
     */
    @Column(name = "special_handling_reason", length = 500)
    private String specialHandlingReason;

    /**
     * 是否标记为异常
     */
    @Column(name = "is_flagged_as_abnormal")
    private Boolean isFlaggedAsAbnormal;

    /**
     * 异常标记原因
     */
    @Column(name = "abnormal_flag_reason", length = 500)
    private String abnormalFlagReason;

    /**
     * 标记异常的教师ID
     */
    @Column(name = "flagged_by_teacher_id", length = 64)
    private String flaggedByTeacherId;

    /**
     * 标记异常时间
     */
    @Column(name = "flagged_time")
    private LocalDateTime flaggedTime;

    /**
     * 是否已复核
     */
    @Column(name = "is_reviewed")
    private Boolean isReviewed;

    /**
     * 复核教师ID
     */
    @Column(name = "reviewer_id", length = 64)
    private String reviewerId;

    /**
     * 复核时间
     */
    @Column(name = "review_time")
    private LocalDateTime reviewTime;

    /**
     * 复核意见
     */
    @Column(name = "review_comment", length = 1000, columnDefinition = "TEXT")
    private String reviewComment;

    /**
     * 评分标准ID
     */
    @Column(name = "scoring_standard_id", length = 64)
    private String scoringStandardId;

    /**
     * 参考答案ID
     */
    @Column(name = "reference_answer_id", length = 64)
    private String referenceAnswerId;

    /**
     * 批注数量
     */
    @Column(name = "annotation_count")
    private Integer annotationCount;

    /**
     * 是否有协作批阅
     */
    @Column(name = "has_collaborative_grading")
    private Boolean hasCollaborativeGrading;

    /**
     * 协作教师ID列表（JSON格式）
     */
    @Column(name = "collaborator_ids", length = 500)
    private String collaboratorIds;

    /**
     * 批阅轮次
     */
    @Column(name = "grading_round")
    private Integer gradingRound;

    /**
     * 之前得分（用于修改记录）
     */
    @Column(name = "previous_score", precision = 5, scale = 2)
    private Double previousScore;

    /**
     * 修改原因
     */
    @Column(name = "score_change_reason", length = 500)
    private String scoreChangeReason;

    /**
     * 修改时间
     */
    @Column(name = "score_change_time")
    private LocalDateTime scoreChangeTime;

    /**
     * 批阅人
     */
    @Column(name = "graded_by", length = 64)
    private String gradedBy;

    /**
     * 批阅时间
     */
    @Column(name = "graded_time")
    private LocalDateTime gradedTime;

    /**
     * 版本号（用于乐观锁）
     */
    @Version
    @Column(name = "version", nullable = false)
    private Integer version;

    /**
     * 扩展属性（JSON格式）
     */
    @Column(name = "additional_attributes", length = 2000, columnDefinition = "TEXT")
    private String additionalAttributes;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    // ================================
    // JPA 关联关系
    // ================================

    /**
     * 关联的批阅任务
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "task_id", insertable = false, updatable = false)
    private OnlineGradingTask gradingTask;

    /**
     * 关联的批注列表
     */
    @OneToMany(mappedBy = "gradingImage", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Annotation> annotations;

    // ================================
    // 枚举已迁移
    // ================================
    // 嵌入枚举已迁移到通用枚举包或专用枚举包
}